<template>
    <section>

        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="query">

                <el-form-item>
                    <el-input v-model="query.keywords" placeholder="宠物"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-select clearable v-model="query.state" placeholder="状态">
                        <el-option label="下架" value="0"></el-option>
                        <el-option label="上架" value="1"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" v-on:click="search">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="danger" @click="onSale" :disabled="this.sels.length===0">上架</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="danger" @click="offSale" :disabled="this.sels.length===0">下架</el-button>
                </el-form-item>
            </el-form>
        </el-col>


    <el-table :data="listData" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column type="index" width="60">
        </el-table-column>
        <el-table-column prop="name" label="宠物名" width="120" sortable>
        </el-table-column>
        <el-table-column prop="saleprice" label="出售价格" width="100" sortable>
        </el-table-column>
        <el-table-column prop="offsaletime" label="下架时间" width="100" sortable>
        </el-table-column>
        <el-table-column prop="onsaletime" label="上架时间" width="100" sortable>
        </el-table-column>
        <el-table-column prop="costprice" label="成本价格" width="100" sortable>
        </el-table-column>

        <el-table-column prop="state" label="状态" width="120">
            <template scope="scope">
                <span v-if="scope.row.state==0" style="color: #8c939d">下架</span>
                <span v-if="scope.row.state==1" style="color: #13ce66">上架</span>

            </template>

        </el-table-column>


        <el-table-column label="操作" width="150">
            <template scope="scope">
                <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
            </template>
        </el-table-column>
    </el-table>

        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="query.pageSize" :total="total" style="float:right;">
            </el-pagination>
        </el-col>


        <!--新增界面-->
        <el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
            <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
                <el-form-item label="宠物名称" prop="name">
                    <el-input v-model="addForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="宠物出售价格" prop="saleprice">
                    <el-input v-model="addForm.saleprice" auto-complete="off"></el-input>
                </el-form-item>


                <el-form-item label="成本价格" prop="costprice">
                    <el-input v-model="addForm.costprice" auto-complete="off"></el-input>
                </el-form-item>


                <el-form-item label="状态" prop="state">
                    <el-radio-group v-model="addForm.state">
                        <el-radio class="radio" :label="0">下架</el-radio>
                    </el-radio-group>
                    <el-radio-group v-model="addForm.state">
                        <el-radio class="radio" :label="1">上架</el-radio>
                    </el-radio-group>
                </el-form-item>


                <el-form-item label="宠物图片" >
                    <el-upload
                            class="upload-demo"
                            action="/api/fastDfs/upload"
                            :on-remove="handleRemove"
                            :on-success="handleSuccess"
                            :file-list="fileList"
                            list-type="picture">
                        <el-button size="small" type="primary">点击上传</el-button>
                        <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
                    </el-upload>
                </el-form-item>


                <el-form-item label="宠物介绍" prop="intro">

                    <quill-editor v-model="addForm.detail.intro" :options="quillConfig"></quill-editor>
                </el-form-item>

                <el-form-item label="领养须知" prop="adoptNotice">

                    <quill-editor v-model="addForm.detail.adoptNotice" :options="quillConfig"></quill-editor>
                </el-form-item>






            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                 <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>



        </section>
</template>

<script>
    import { quillEditor } from "vue-quill-editor"; //调用编辑器
    import 'quill/dist/quill.core.css';
    import 'quill/dist/quill.snow.css';
    import 'quill/dist/quill.bubble.css';

    import quillConfig from '../../common/js/quill-config'

    export default {

        components: {
            quillEditor
        },


        data(){
            return{
                fileList:[],
                quillConfig:quillConfig,
                listLoading:false,
                listData:[],
                sels:[],
                total:0,
                query:{
                    pageSize:5,
                    currentPage:1,
                    keywords:'',
                    state:null
                },
                addFormVisible:false,//编辑界面是否显示出来
                addLoading:false,// 避免操作数据加载不出来卡住
                addFormRules:{

                    name: [
                        { required: true, message: '用户名称', trigger: 'name' }
                    ],
                    state: [
                        { required: true, message: '状态', trigger: 'state' }
                    ]
                },
                addForm:{

                    name: '',
                    saleprice:0,
                    resources:'',
                    state:0,
                    costprice:0,
                    type_id:1,


                    detail:{

                        adoptNotice:'',
                        intro:"",

                    }


                },

            }
        },
        methods:{

            offSale(){

                // 遍历获取到的ids的一个数组
                var ids = this.sels.map(item => item.id);
                this.$confirm('此操作将下架宠物, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    // 发送patch请求需要传递参数，将上面取出来的ids数组对象传入
                    this.$http.patch("/pet/offSale",ids).then(res=>{
                        // 结构表达式取出data中的参数，用来做提示用语
                        let{success,message}=res.data;
                        if(success){
                            this.$message({
                                type: 'success',
                                message: '下架成功!'
                            });
                        }else {
                            this.$message({
                                type: 'info',
                                message: '下架失败，操作异常'
                            });
                        }
                        // 不管删除失败还是成功都要刷新页面
                        this.loadListData();
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消下架'
                    });
                });


            },


            onSale(){

                // 遍历获取到的ids的一个数组
                var ids = this.sels.map(item => item.id);
                this.$confirm('此操作将上架宠物, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    // 发送patch请求需要传递参数，将上面取出来的ids数组对象传入
                    this.$http.patch("/pet/onSale",ids).then(res=>{
                        // 结构表达式取出data中的参数，用来做提示用语
                        let{success,message}=res.data;
                        if(success){
                            this.$message({
                                type: 'success',
                                message: '上架成功!'
                            });
                        }else {
                            this.$message({
                                type: 'info',
                                message: '上架失败，操作异常'
                            });
                        }
                        // 不管删除失败还是成功都要刷新页面
                        this.loadListData();
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消上架'
                    });
                });




            },


            // 上传文件的方法
            handleSuccess(response, file, fileList){
            var str ="";
            for(let i =0;i<fileList.length; i++){
                // 最后一个元素时
                if(i == fileList.length-1){
                    str+=fileList[i].response.resultObj;
                }else {
                    str+=fileList[i].response.resultObj+",";

                }
                this.addForm.resources=str;

            }







            },
            // 文件移除
            handleRemove( file, fileList){




            },



            // 显示全部数据
            loadListData(){
                // 将查询到的数据都展示出来
                this.$http.post("/pet",this.query).then(res=>{
                    console.log(res);
                    // 赋值的过程
                    this.listData=res.data.data;
                    this.total =res.data.total;

                })

            },
            selsChange(val){
                console.log(val)
               // sels只要有数据就能点击
                this.sels=val;
            },
            formatSex: function (row, column) {
                return row.state == 1 ? '启用' : row.state == 0 ? '禁用' : '未知';
            },
            // 批量删除
            batchRemove(){
                // 遍历获取到的ids的一个数组
                var ids = this.sels.map(item => item.id);
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    // 发送patch请求需要传递参数，将上面取出来的ids数组对象传入
                    this.$http.patch("/pet",ids).then(res=>{
                        // 结构表达式取出data中的参数，用来做提示用语
                        let{success,message}=res.data;
                        if(success){
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                        }else {
                            this.$message({
                                type: 'info',
                                message: '删除失败，操作异常'
                            });
                        }
                        // 不管删除失败还是成功都要刷新页面
                        this.loadListData();
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });




            },
            handleCurrentChange(val){
                console.log(val)
                this.query.currentPage=val;
                this.loadListData();

            },
            // 编辑方法
            handleEdit(index,row){
                //数据拷贝
                this.addForm=Object.assign({}, row);
                this.fileList=[];
                let picArr =row.resources.split(",");
                for (let i =0;i<picArr.length;i++){
                    let obj ={
                        name:"图片"+(i+1),
                        url:"http://47.95.117.210:8888"+picArr[i]
                    }
                    this.fileList.push(obj);
                }
                this.addFormVisible=true;
            },
            // 高级查询方法
            search(){
                // 初始化当前页
                this.query.currentPage=1;
                this.loadListData();
            },
            // 新增方法
            handleAdd(){
                this.addForm={
                    name: '',
                    saleprice:0,
                    resources:'',
                    state:0,
                    costprice:0,
                    type_id:1,


                    detail:{

                        adoptNotice:'',
                        intro:"",

                    }

                }
                // 清空图片
                this.fileList=[];

                this.addFormVisible = true;


            },
            handleDel(index,row){

                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    // 把row对象中的id取出来
                     let id = row.id;
                     // 字符串拼接id，http://localhost:8080/dept/9,dept后面还有一个/和拼接了id
                     this.$http.delete("/pet/"+id).then(res=>{
                           let{success,message}=res.data;
                                if(success){
                                    this.$message({
                                        type: 'success',
                                        message: '删除成功!'
                                    });

                                }else {
                                    this.$message({
                                        type: 'info',
                                        message: '删除失败，操作异常'
                                    });
                                }
                                    // 不管删除失败还是成功都要刷新页面
                                    this.loadListData();
                             })


                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消删除'
                        });
                    });

            },
            addSubmit(){
                // 开启添加框
                this.addFormVisible = true;
                this.$http.put("/pet",this.addForm).then(res=>{
                    // 解构 拿出两个参数
                    let{success,message}=res.data;
                    if(success){
                        this.$message({
                            type: 'success',
                            message: '操作成功!'
                        });

                    }else {
                        this.$message({
                            type: 'info',
                            message: '操作失败，操作异常'
                        });
                    }
                    // 不管删除失败还是成功都要刷新页面
                    this.addFormVisible=false;
                    this.loadListData();
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '保存失败'
                    });
                });

            }


        },

        mounted(){

        this.loadListData();

        }



    }

</script>

<style >

</style>